package com.atguigu.distributed.lock.zookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author linjy on 2022/10/18
 */
public class ZkTest {

    public static void main(String[] args) throws KeeperException, InterruptedException {


        CountDownLatch countDownLatch = new CountDownLatch(1);
        // 获取zookeeper链接
        ZooKeeper zooKeeper = null;
        try {
            zooKeeper = new ZooKeeper("Hadoop102:2181", 30000, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    //是否获取链接,节点发生变化也会走这里,要加多个类型判断
                    if (Event.KeeperState.SyncConnected.equals(event.getState())
                            && Event.EventType.None.equals(event.getType())) {
                        System.out.println("获取链接成功。。。。。。" + event);
                        countDownLatch.countDown();
                        //zookeeper版本不同，此处有些不一样
//                    }else if(Event.KeeperState.Closed.equals(event.getState())){
//                        System.out.println("关闭链接。。。。。。" + event);
                    }else{
                        System.out.println("节点事件。。。。。。");
                    }
                }
            });

            countDownLatch.await();



        // 创建一个节点，1-节点路径 2-节点内容 3-节点的访问权限 4-节点类型
        // 3-节点的访问权限:
        // OPEN_ACL_UNSAFE：任何人可以操作该节点
        // CREATOR_ALL_ACL：创建者拥有所有访问权限
        // READ_ACL_UNSAFE: 任何人都可以读取该节点
        // zooKeeper.create("/atguigu/aa", "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zooKeeper.create("/test", "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        // zooKeeper.create("/atguigu/cc", "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
        // zooKeeper.create("/atguigu/dd", "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        // zooKeeper.create("/atguigu/dd", "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        // zooKeeper.create("/atguigu/dd", "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);

        // 判断节点是否存在
        Stat stat = zooKeeper.exists("/test", true);
        if (stat != null){
            System.out.println("当前节点存在！" + stat.getVersion());
        } else {
            System.out.println("当前节点不存在！");
        }

        // 判断节点是否存在，同时添加监听
//        zooKeeper.exists("/test", event -> {
//        });

        // 获取一个节点的数据
        byte[] data = zooKeeper.getData("/aa", false, null);
        System.out.println(new String(data));

        // 查询一个节点的所有子节点
        List<String> children = zooKeeper.getChildren("/zookeeper", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("节点的子节点发生变化。。。。");
            }
        });
        System.out.println(children);

        // 更新:版本号必须和当前节点的版本号一致，否则更新失败。也可以指定为-1，代表不关心版本
        zooKeeper.setData("/aa", "wawa...".getBytes(), -1);

        // 删除一个节点
        //zooKeeper.delete("/aa/bb", -1);


        System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (zooKeeper != null){
                zooKeeper.close();
            }
        }
    }
}
